Page({
  data: {
    buttonList: [ // 按钮组
      { text: '提交' },
      { text: '委托记录' }
    ],
    index: '',
    index2: '',
    printTypeList: [], // 打印类型列表
    printTypeList2: [],
    form: {
      userId: '',
      printType: '',
      number: ''
    },
    inputListArray: [],
    inputListas: [],
    inputList: [
      {
        id: 0,
        name: "打印类型"
      },
    ],
  },
  onLoad () {
    this.getPrintType()
  },
  /**
   * @see 获取委托打印
   */
  getPrintType () {
    wx.$API['workService/getPrintType']({}).then((result) => {
      this.setData({ printTypeList: result.data, printTypeList2: result.data })
    }).catch((err) => {
      wx.showToast({
        title: '获取打印类型失败',
        icon: 'none',
        duration: 2000
      })
    })
  },

  AddInput (e) {
    var inputList = this.data.inputList;
    var newData = { id: inputList.length };  //这个用来动态添加id为对应表单个数的对象
    inputList.push(newData);  //给formitems添加1个对象
    this.setData({
      inputList: inputList, //动态渲染
    })

  },



  /**
   * @see 提交按钮
   */
  submit () {
    const { userId, printType, number } = this.data.form
    if (!userId || !printType || !number) {
      wx.showToast({
        title: '请完善资料',
        icon: 'none',
        duration: 2000
      })
      return
    }
    const request = {
      printType,
      payCount: number,
      beEntrutsUserId: userId,
    }
    wx.$API['workService/entrustPrintCommit'](request).then((result) => {
      if (result.data) {
        const data = JSON.stringify({
          type: 'success', // success warn
          title: '委托成功', // 打印委托
          info: '',
          btnConfirm: true,
          btnOthers: [
            { text: '委托记录', url: '/pages/workService/components/recordList/index?type=委托打印' }
          ]
        })
        // 跳转到状态页面
        wx.navigateTo({
          url: '/pages/workService/components/status/index?data=' + encodeURIComponent(data)
        })
      } else {
        wx.showToast({
          title: '错误',
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((err) => {
      wx.showToast({
        title: err,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * @see 跳转委托记录
   */
  onButton (event) {
    const { index } = event.detail
    switch (index) {
      case 0:
        this.submit()
        break
      case 1:
        wx.navigateTo({
          url: '/pages/workService/components/recordList/index?type=委托打印'
        })
        break
    }

  },
  /**
   * @see 选择-打印类型
   */
  bindPickerChange (e) {
    const { value } = e.detail
    // console.log(e.detail, "拿到当前选中值");
    // console.log(this.data.printTypeList[value].vcPrintTypeId, "拿到value值")
    this.setData({
      'form.printType': this.data.printTypeList[value].vcPrintTypeId,
      index: value
    })
  },

  bindPickerChange2 (e) {
    const { value } = e.detail
    this.setData({
      'form.printType': this.data.printTypeList2[value].vcPrintTypeId,
      index2: value
    })
  },
  /**
   * @see 输入-受委托人学号
   */
  bindKeyInput (e) {
    const { currentTarget, detail } = e
    this.setData({ ['form.' + currentTarget.dataset.name]: detail.value })
  },
  /**
   * @see 输入-份数（最大10份）
   */
  bindinput ({ detail }) {
    const { value } = detail
    if (value.length === 1) {
      const number = value.replace(/[^1-9]/g, '')
      this.setData({
        'form.number': number
      })
      return number
    } else {
      const number = value.replace(/\D/g, '')
      this.setData({
        'form.number': number
      })
      return number
    }
  }
})